﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using Newtonsoft.Json;

namespace MapAPIControl
{
    /// <summary>
    /// Interaction logic for Map.xaml
    /// </summary>
    public partial class Map : UserControl
    {
        //        {
        //    address: "CN|北京|北京|None|UNICOM|1|None", # 地址
        //    content: {                                  # 详细内容
        //    address_detail: {         # 详细地址信息
        //        province: "北京市",   # 省份
        //        city: "北京市",       # 城市
        //        district: "",        # 区县
        //        street: "",          # 街道
        //        street_number: "",   # 门址
        //        city_code: 131       # 百度城市代码
        //    },
        //    address: "北京市",       # 简要地址
        //    point: {                  # 百度坐标系
        //        y: "4828090.32",
        //        x: "12957244.92"
        //    }
        //    },
        //    status: 0   # 返回状态码
        //}
        dynamic location;
        static string ak = "101a124f65cb0122fdc49036fe924f42";
        public string CurrentCity { get; set; }
        public Map()
        {
            InitializeComponent();
        }
        public void Initial()
        {
            var src = System.Text.Encoding.UTF8.GetString(Resource1.yl);
            wb.NavigateToString(src);
        }
        private static string getIP()
        {
            string ip;
            #region get ip
            var req = WebRequest.Create("http://iframe.ip138.com/ic.asp");
            var rep = req.GetResponse();
            var sr = new StreamReader(rep.GetResponseStream(), Encoding.GetEncoding("GB2312"));
            var content = sr.ReadToEnd();
            string ptn = @"\[(\d+\.\d+\.\d+\.\d+)\]";
            var regx = new Regex(ptn);
            var m = regx.Match(content);
            ip = m.Groups[1].Value;
            #endregion
            return ip;
        }

        string reqGET(string url)
        {
            var req = WebRequest.Create(url);
            var s = req.GetResponse().GetResponseStream();
            using (var sr = new StreamReader(s))
            {
                return sr.ReadToEnd();
            }
        }

        private void wb_LoadCompleted(object sender, NavigationEventArgs e)
        {
            string ip;
            ip = getIP();
            //获取IP所在城市，然后让浏览器切换到该城市。
            var reqUrl = string.Format("http://api.map.baidu.com/location/ip?ak={0}&ip={1}", ak, ip);
            location = JsonConvert.DeserializeObject(reqGET(reqUrl));
            CurrentCity = location.content.address_detail.city;
            wb.InvokeScript("initMapByProjection", location.content.point.x, location.content.point.y);
            if (LoadCompleted != null)
            {
                LoadCompleted(CurrentCity);
            }
        }
        /// <summary>
        /// 传出当前城市，还可以传更多东西的。
        /// </summary>
        public event Action<string> LoadCompleted;
        public void CenterAndZoom(string lat, string lang)
        {
            wb.InvokeScript("centerAndZoom", lat, lang);
        }
    }
}
